<template>
  <transition name="fade">
    <div v-if="localVisible" class="dialog-mask" @click.self="onMaskClick">
      <div class="dialog-box">
        <div class="dialog-title">{{ title }}</div>
        <div class="dialog-content">{{ content }}</div>
        <div class="dialog-footer">
          <button @click="$emit('cancel')">取消</button>
          <button @click="$emit('confirm')">确认</button>
        </div>
      </div>
    </div>
  </transition>
</template>

<script setup>
import { ref, watch } from 'vue'

const props = defineProps({
  visible: Boolean,
  title: String,
  content: String,
  maskClosable: { type: Boolean, default: true }
})

const emit = defineEmits(['update:visible', 'confirm', 'cancel'])

const localVisible = ref(props.visible)

watch(
  () => props.visible,
  (newVal) => {
    localVisible.value = newVal
  }
)

const onMaskClick = () => {
  if (props.maskClosable) {
    emit('update:visible', false)
    emit('cancel')
  }
}
</script>

<style scoped>
.dialog-mask {
  position: fixed;
  top: 0; left: 0;
  width: 100%; height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.dialog-box {
  background-color: rgba(var(--background-color), 0.9); /* 与 ChatPage 一致 */
  backdrop-filter: blur(8px);
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  padding: 20px;
  min-width: 300px;
  max-width: 90vw;
  color: rgb(var(--text-color));
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
}

.dialog-title {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 10px;
}

.dialog-content {
  font-size: 14px;
  margin-bottom: 20px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.dialog-footer button {
  font-size: 14px;
  padding: 6px 16px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease-in-out;
  border: none;
}

.dialog-footer button:hover {
  opacity: 0.9;
}

/* 取消按钮 */
.dialog-footer button:nth-child(1) {
  background-color: rgba(var(--text-color), 0.1);
  color: rgb(var(--text-color));
}

/* 确认按钮 */
.dialog-footer button:nth-child(2) {
  background-color: rgb(var(--primary-color));
  color: #ffffff;
}

/* 过渡动画 */
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
